<template>
	<div class="authorize">
		<bread-crumb>
			<template slot="node">
				<li>
					<router-link link to="/uc/MyInvolved">我參與的投票</router-link>
				</li>
			</template>
			投票詳情
		</bread-crumb>
		<div class="uc-body">
			<div class="manage-detail">
				<div class="c-red">在投票失敗、投票被作廢、投票通過且賽事開獎後，方能取回投票的票數。</div>
				<div style="height: 20px;"></div>
				<manage-detail
					:vote-object="voteObject"
					:timely-object-vote="timelyObjectVote"
					:timely-object-contest="timelyObjectContest"
					:review-object-contest="reviewObjectContest"
					:loading-vote="loadingVote"
					:loading-contest="loadingContest"
				>
				</manage-detail>

				<!--领取信息-->
				<el-divider content-position="left">可用操作</el-divider>
				<div class="manage-detail-row" v-if="numberVotesToBeReceived>0">
					<!--赛事完成了，足球收益能领取也敲定了，可以算出来足球应该给多少了。显示出来完整收益-->
					<div
						v-html="receivableAmount"></div>
				</div>

				<!--***************************************控制按钮***************************************-->
				<!--无论时投票成功，还是失败，一键领取后，票数归零。判断自己可领取数量，来按钮就显示隐藏了-->
				<div v-if="numberVotesToBeReceived>0">
					<div class="btn-group">

						<!--投票失敗-->
						<div v-if="timelyObjectVote.status==3">
							<el-button type="warning" @click="getBenefits">失敗取回票數</el-button>
						</div>

						<!--投票作廢-->
						<div v-if="timelyObjectVote.status==2">
							<el-button type="danger" @click="getBenefits">作廢取回票數與基本金</el-button>
						</div>

						<!--投票成功賽事開啟，並且覆核已通過 -->
						<div v-if="reviewObjectContest.reviewState == 7 && timelyObjectVote.status==7 ">
							<el-button type="danger" @click="getBenefits">一鍵領取票數、與獎勵</el-button>
						</div>
					</div>
				</div>
			</div>
		</div>
		<!--******************************************* 结果填写弹层 ***********************************************-->

	</div>
</template>

<script>

//数据
import breadCrumb from '../components/breadCrumb.vue'
import manageDetail from "./components/manageDetail";
import { mixins } from '@/mixins/mixins'
import { mixinsUtils } from '@/mixins/mixins.utils'
import { mixinsManage } from "@/views/manage/js/manage.mixins"
import { contestMixinsGlobal } from '@/views/contestHall/contest.mixins.global'
import {mixinsVoteDetail} from "./js/mixins.vote.detail";


import {
 tronTokenAnalyze, tronVotingIncomeAmount
} from "./js/manage.user.lib";

import {dev} from "@/libs/tron.lib";
import { getEffectiveValueForContest2 } from "@/views/manage/js/manage.func";
import {hex2Num } from '@/libs/tron.lib'
import { apiRequestGameTron, tronGetGameKeyData } from "@/views/contestHall/contest.func"; //vuex工具
//导出
export default {
	name      : "manageMyInvolvedDetail",
	components: { breadCrumb,manageDetail },
	mixins: [mixins,  mixinsUtils, mixinsManage, contestMixinsGlobal, mixinsVoteDetail],
	data () {
		return {
			//////////////////////简单值////////////////////////////
			earningsText:"",//收益文本描述

			receivableAmount:"",//能领取到的资金

			//////////////////////复杂值////////////////////////////

			//提交数据对象
			voteInput:{
				score1:1,
				score2:1,
			}
		}
	},
	computed:{},
	created () {
		//数据加载入口
		this.getMySponsoredVoteDetail(()=> {
			this.dataReadDispatch();
		}) //请求数据
	},
	mounted () {
	},
	methods: {

		// 重新加载数据（我发起的和我参与的这里不一样）
		dataReadDispatch() {
			this.checkDecimal();	//检查精度加载
			this.viewProposalInfo([this.id]);//查看投票信息
			this.getTimelyDataOfContestFromChain(this.playId);//读取赛事实时数据
			this.getReviewInfo(this.id);//查看复审信息。有则有,没有则没有
			this.getBalanceOfVotes();	//获取我能领取的金额，一键领了就空了，变量
		},

		//读取本游戏支持的币种信息
		checkDecimal() {
			let that = this;

			//获取币种精度
			let bet_decimal = that.betCoinData.decimal;
			if (!that.betCoinData.decimal || parseInt(bet_decimal) <= 0) {
				console.log('正在检查币种信息...');
				tronTokenAnalyze(this.timelyObjectContest.betToken, this.sender, (o)=>{
					that.betCoinData = o
					this.getVotingIncomeAmount();	//获取我能领取的金额
				})
				//开启递归调用
				this.contractInfoTime = setTimeout(() => {
					that.checkDecimal()
				}, 5000);
				return;
			} else {
				this.getVotingIncomeAmount();	//获取我能领取的金额
			}
		},

		//todo 整改提取
		//读取赛事实时数据并解析
		getTimelyDataOfContestFromChain(id){
			this.loadingContest = true
			//从赛事合约请求实时数据并解析
			tronGetGameKeyData(id).then((rs) => {
				// let {param} = rs
				let _analyzing2 = getEffectiveValueForContest2(rs)
				dev && console.log('/////////////////////////////////////////////请求单个赛事实时数据解析结果数据:',_analyzing2)
				this.timelyObjectContest.status = _analyzing2[1] //取得状态
				this.timelyObjectContest.odds = _analyzing2[2] //取得P率

				//以下位数截取仅供胜平负参考，因为随着赔率长度不一样，返回长度不bytes一样。需注意
				let matchResults = (rs + "").substring(1408, 1472);
				//10的0次方等于1，除以1数字不变。使用该方法+零次方目的：使用hex转换数字的功能，并保持数值不做次方改变
				matchResults = hex2Num(matchResults, 0);
				this.timelyObjectContest.winning = matchResults;

				this.loadingContest = false
			})
		},

		//////////////////////////////////////////////// 交互 /////////////////////////////////////////////////
		//////////////////////////////////////////////// 交互 /////////////////////////////////////////////////
		//////////////////////////////////////////////// 交互 /////////////////////////////////////////////////


		// 查看可以拿到的金额，并解析（发起人和投票人不一样）
		getVotingIncomeAmount() {
			let that = this;
			let id = this.id;
			tronVotingIncomeAmount(this.sender, this.sender, id, (rs) => {
					console.log(rs,rs)
					//解析合约返回（目前使用服务器的接口来解析）
					apiRequestGameTron({
						"type": "votingIncomeAmount",
						'act': 'returnDataAnalyze',
						"param": rs
					}).then(result => {
						let data = result;
						if (data.resultFlag != true) {
							return;
						}
						let body = data.data;
						let decimal_ = that.betCoinData.decimal;
						let symbol_ = that.betCoinData.symbol;

						//xg 质押票，8个精度。收益的赛事，usdt 6个精度
						let control = (parseInt(body.locked) / 100000000).toFixed(3);
						let income = (parseInt(body.transfer_amount_) / Math.pow(10, decimal_)).toFixed(3);

						//寫入治理領取金額
						let earningsText = "";
						earningsText = `<div>質押票數：${control} ${that.xg.name} （贊成票+反對票）</div>`;
						//投票完成了，可以知道足球的收益了，近而算出足球領取多少
						if (that.reviewObjectContest.reviewState == 7) {
							earningsText += "<div>賽事收益：" + income + symbol_ + "</div>";
						}else{
							earningsText += "<div>賽事收益：賽事成功開啟且覆核後才有此數據</div>";
						}
						that.receivableAmount = earningsText;
					}).catch(err => {
						console.log(err)
					})
				}
			);
		},

		////////////////////////////////////////////////其它/////////////////////////////////////////////////
		////////////////////////////////////////////////其它/////////////////////////////////////////////////
		////////////////////////////////////////////////其它/////////////////////////////////////////////////
		//刷新，筛选
		reFresh () {
		},
	},
	beforeDestroy () {
		window.clearTimeout(this.contractInfoTime);
		clearInterval(this.timerProposal)
	},
}
</script>

<style lang="less" scoped>
/*公共变量*/
@import '~@/assets/global.less';

//单独控制弹层样式细节
.dialog-detail-module {
	.module-wrap {
		.v-row {padding:3px 0;}
	}
	.btn-group{
	}
}
/************************************适应***************************************/
@media screen and (max-width: @MINSCREEN) {

}
</style>
